package com.jeasonzhao.commons.db;

import com.jeasonzhao.commons.utils.DataTypes;

public class SqlTypesHelper
{
    private SqlTypesHelper()
    {
        super();
    }

    public static final DataTypes getDataTypesFrom(int n)
    {
        if(isDate(n))
        {
            return DataTypes.DATE;
        }
        else if(isInteger(n))
        {
            return DataTypes.INT;
        }
        else if(isDouble(n))
        {
            return DataTypes.DOUBLE;
        }
        else if(n == java.sql.Types.BOOLEAN)
        {
            return DataTypes.BOOL;
        }
        else if(isString(n))
        {
            return DataTypes.STRING;
        }
        else
        {
            return DataTypes.OBJECT;
        }
    }

    public static final int getSQLTypeFromClass(Class<?> cls)
    {
        if(null == cls)
        {
            return java.sql.Types.VARCHAR;
        }
        DataTypes dt = DataTypes.from(cls);
        if(dt.equals(DataTypes.BOOL))
        {
            return java.sql.Types.BOOLEAN;
        }
        else if(dt.equals(DataTypes.DATE))
        {
            return java.sql.Types.TIMESTAMP;
        }
        else if(dt.equals(DataTypes.DOUBLE))
        {
            return java.sql.Types.DOUBLE;
        }
        else if(dt.equals(DataTypes.INT))
        {
            return java.sql.Types.INTEGER;
        }
        else
        {
            return java.sql.Types.VARCHAR;
        }
    }

    public static final boolean isDate(int nType)
    {
        return nType == java.sql.Types.DATE ||
            nType == java.sql.Types.TIME ||
            nType == java.sql.Types.TIMESTAMP;
    }

    public static final boolean isDouble(int nType)
    {
        return nType == java.sql.Types.BIGINT ||
            nType == java.sql.Types.BIT ||
            //nType == java.sql.Types.BOOLEAN ||
            nType == java.sql.Types.DOUBLE ||
            nType == java.sql.Types.DECIMAL ||
            nType == java.sql.Types.FLOAT ||
            nType == java.sql.Types.INTEGER ||
            nType == java.sql.Types.NUMERIC ||
            nType == java.sql.Types.REAL ||
            nType == java.sql.Types.SMALLINT ||
            nType == java.sql.Types.TINYINT;
    }

    public static final boolean isInteger(int nType)
    {
        return nType == java.sql.Types.BIGINT ||
            nType == java.sql.Types.BIT ||
            //nType == java.sql.Types.BOOLEAN ||
            nType == java.sql.Types.DECIMAL ||
            nType == java.sql.Types.INTEGER ||
            nType == java.sql.Types.SMALLINT ||
            nType == java.sql.Types.TINYINT;
    }

    public static final boolean isString(int nType)
    {
        return nType == java.sql.Types.CHAR ||
            nType == java.sql.Types.VARCHAR;
    }

}
